package com.mansoor.app.islamicnameslite.activities;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import com.mansoor.app.islamicnameslite.beans.Name;
import com.mansoor.app.islamicnameslite.db.NamesDao;
import com.mansoor.app.islamicnameslite.services.RestClient;
import com.mansoor.app.islamicnameslite.services.RestClientImpl;
import org.json.JSONException;

import java.io.IOException;

import static com.mansoor.app.islamicnameslite.util.AppConstant.*;

/**
 * Created by IntelliJ IDEA.
 * User: TKMAQ61
 * Date: Oct 19, 2009
 * Time: 4:10:18 PM
 */
public class NameDetail extends Activity
{
    private Name name;
    private RestClient client = new RestClientImpl();

    private final Handler handler = new Handler()
    {
        public void handleMessage(final Message msg)
        {
            if (name == null)
            {
                showDialog(ALERT);
            }
            else
            {
                dismissDialog(PROGRESS);
                populateResult(name.getName(), name.getGender(), name.getMeaning());
            }

        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        showDialog(PROGRESS);

        ServiceThread t = new ServiceThread();
        t.start();

    }

    private class ServiceThread extends Thread
    {
        @Override
        public void run()
        {
            super.run();    //To change body of overridden methods use File | Settings | File Templates.
            try
            {
                name = findName((String)getIntent().getExtras().get("name"));
                handler.sendEmptyMessage(0);
            }
            catch (Exception e)
            {
                dismissDialog(PROGRESS);
                showDialog(ERROR);
            }

        }
    }

    private Name findName(String name) throws IOException, JSONException
    {
        return client.getName(name); //To change body of created methods use File | Settings | File Templates.
    }


    public void populateResult(String name, String gender, String meaning)
    {
        TextView nameview = (TextView) findViewById(R.id.name);
        TextView genderview = (TextView) findViewById(R.id.gender);
        TextView meaningview = (TextView) findViewById(R.id.meaning);

        nameview.setText(name);
        genderview.setText(gender);
        meaningview.setText(meaning);

    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        Dialog dialog = null;
        switch (id)
        {
              case PROGRESS:
                ProgressDialog progressDialog = new ProgressDialog(NameDetail.this);

                progressDialog.setTitle("Fetching");
                progressDialog.setMessage("Name");
              //  progressDialog.setIndeterminate(true);
                dialog = progressDialog;
        }

        return dialog;

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.detail_menu, menu);

        return true;   //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();
        switch (id)
        {
            case R.id.favorate:
                addFavorate();
                return true;
        }
        return false;
    }

    private void addFavorate()
    {

        NamesDao dao = new NamesDao(this);
        if (!name.getName().equalsIgnoreCase("Name Not Found"))
        {
            dao.storeFavorate(name);
            dao.cleanup();
        }
        //       put(IS_FAV, true);
       /* final Intent intent = new Intent(this, NameDetail.class);
        startActivity(intent);*/


    }

}
